package com.ruicar.afs.cloud.apply.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.business.condition.WorkflowAgencyTaskCondition;
import com.ruicar.afs.cloud.apply.business.service.WorkflowAgencyTaskService;
import com.ruicar.afs.cloud.apply.common.entity.WorkflowAgencyTask;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.parameter.commom.enums.AProveBusinessTypeEnum;
import com.ruicar.afs.cloud.parameter.commom.enums.OrderOprType;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 工作流待办任务controller
 *
 * @author maxueyu
 */
@RestController
@Slf4j
@ApiOperation("工作流待办任务controller")
@AllArgsConstructor
@RequestMapping("/workflow")
public class WorkflowAgencyTaskController {

    private final WorkflowAgencyTaskService workflowAgencyTaskService;

    /**
     * 更新常规审批流程的退回（修订暂停）待办任务的打开状态
     * @param condition
     * @return
     */
    @RequestMapping("/updateBackTaskIsOpen")
    @ApiOperation("更新常规审批流程的退回（修订暂停）待办任务的打开状态")
    public IResponse<Boolean> getApplyConditionApprovalInfo(@RequestBody WorkflowAgencyTaskCondition condition) {
        List<WorkflowAgencyTask> workflowAgencyTaskList = workflowAgencyTaskService.list(Wrappers.<WorkflowAgencyTask>lambdaQuery()
                .eq(WorkflowAgencyTask::getBusinessNo,condition.getBusinessNo())
                .eq(WorkflowAgencyTask::getBusinessType,String.valueOf(OrderOprType.NORMAL))
                .eq(WorkflowAgencyTask::getStatus, AProveBusinessTypeEnum.BACK.getCode())
                .orderByDesc(WorkflowAgencyTask::getCreateTime));
        if(CollectionUtil.isNotEmpty(workflowAgencyTaskList)){
            WorkflowAgencyTask workflowAgencyTask = workflowAgencyTaskList.get(0);
            workflowAgencyTask.setIsOpen(WhetherEnum.YES.getIndex());
            workflowAgencyTaskService.updateById(workflowAgencyTask);

        }
        return new IResponse<Boolean>().setCode(CommonConstants.SUCCESS);
    }
}
